package cn.wizzer.app.user.modules.service.impl;

import cn.wizzer.app.user.modules.models.Goods;
import cn.wizzer.app.user.modules.services.GoodsService;
import cn.wizzer.framework.base.service.BaseServiceImpl;
import cn.wizzer.framework.page.Pagination;
import com.alibaba.dubbo.config.annotation.Service;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;

import java.util.List;

@IocBean(args = {"refer:dao"})
@Service(interfaceClass = GoodsService.class)
public class GoodsServiceImpl extends BaseServiceImpl<Goods> implements GoodsService {
    public GoodsServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    public Pagination listPage(int pageNumber, int pageSize, String language, String searchKeyword, boolean isPlatform) {
        Sql sql = Sqls.create("select id,language,name,price,stock,coverPic,asBanner,disabled,ctAt,opAt from goods $condition");
        Sql countSql = Sqls.create("select count(1) from goods $condition");
        Cnd cnd = Cnd.NEW();
        if (!isPlatform) {
            cnd.and("disabled", "=", 0);
        }
        if(Strings.isNotBlank(language)){
            cnd.and("language", "=", language);
        }
        if(Strings.isNotBlank(searchKeyword)){
            searchKeyword = "%" + searchKeyword + "%";
            cnd.and("name", "like", searchKeyword);
        }
        cnd.and("delFlag", "=", 0);
        sql.setCondition(cnd);
        sql.appendSourceSql("order by opAt desc");
        countSql.setCondition(cnd);
        return this.listPage(pageNumber, pageSize, sql, countSql);
    }

    @Override
    public List<Goods> queryByLanguage(String language) {
        Sql sql = Sqls.create("SELECT id,coverPic,language,name FROM goods ")
                .appendSourceSql("WHERE language = @language and asBanner = 1 and disabled = 0")
                .setParam("language", language);
        return this.listEntity(sql);
    }

}
